<?php



class XmlDomConstruct extends DOMDocument {

    private $texto = "";
    private $id;
    //$this->loadHTMLFile('http://localhost/untitled-2.html');



    public function json2html($jsonString,$id=0) {
        $this->id=$id;
        //$firephp=  FirePHP::getInstance(true);
        $this->fromMixed(json_decode($jsonString, TRUE));

        $config = array(
            'indent' => true,
            'markup' => true,
            'output-xhtml' => true,
            'indent-cdata' => true,
            'wrap' => 200,
            'preserve-entities' => true,
            'escape-cdata' => true,
            'break-before-br' => true);
        $tidy = new tidy;
        //$firephp->log($this->texto);
        $tidy->parseString($this->texto, $config, 'utf8');
        
        //$tidy->cleanRepair();
        
        //$firephp->log(BeautyJSON::beautify_json($jsonString));
        //$firephp->log($tidy);
                
        //return $tidy;
        return $this->texto;
    }

    public function html2json($htmlString) {
        $this->loadHTML($htmlString);
        
        $books = $this->getElementsByTagName('html')->item(0);
        return json_encode($this->htmltoarray($books));
    }

    public function body2json($htmlString) {
        $this->loadHTML($htmlString);
        
        
        $rootNode = $this->documentElement->tagName;
        
        $books = $this->getElementsByTagName('body')->item(0);

        return json_encode($this->htmltoarray($books));
    }

    public function html2jsonFromDiv($htmlString) {
        $this->loadHTML($htmlString);
        $books = $this->getElementsByTagName('div')->item(0);
        return $this->htmltoarray($books);
    }

    private function htmltoarray($node) {
        $nodes[$node->nodeName] = [];
        $nodes[$node->nodeName]['attributes'] = $this->getattributes($node);
        $nodes[$node->nodeName]['hijos'] = [];
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $n) {
                $x = $this->htmltoarray($n);
                array_push($nodes[$node->nodeName]['hijos'], $x);
            }
        } else {
            if ($node->nodeName == "#text" || $node->nodeName == "#cdata-section") {

                $nodes[$node->nodeName][0] = trim($node->nodeValue);
            }
        }
        return $nodes;
    }

    protected function getattributes($node) {
        if ($node->attributes) {
            $length = $node->attributes->length;
            if ($length) {
                $attributes = [];
                for ($i = 0; $i < $length; ++$i) {
                    $name = $node->attributes->item($i)->name;
                    $attributes[$name] = $node->attributes->item($i)->value;
                    //var_dump($attributes);
                }
                return $attributes;
            }
        }
        return "";
    }

    private function fromMixed($arr = null, /* $texto = null, */ $x = null) {
        
        $x = is_null($x) ? 0 : $x;
        $x++;
        $this->texto = is_null($this->texto) ? "" : $this->texto;
        
        //var_dump($arr);
        foreach ($arr as $key => $value) {
            //var_dump($key);
            //var_dump($value);
            //var_dump($value);
            if ($key == "#text" || $key == "#cdata-section") {
                //echo "entro";
                //var_dump($value);
                $this->texto.="$value[0]";
            } elseif (is_null($value)) {
                $this->texto.="";
            } else {
                $this->texto.="<$key";
                if (is_array($value)) {
                    reset($value);
                    if (isset($value['attributes'])) {
                        if (is_array($value['attributes'])) {
                            if (array_key_exists("class", $value["attributes"])) {
                                if ($value["attributes"]["class"] == "iterable") {
                                    $value["attributes"]["class"] = "not_iterable";
                                    $db=new connection();
                                    $info=new AlaniaInformacionModel();
                                    $info->setTipoContenido($this->id);
                                    foreach (AlaniaInformacionModel::findByExample($db->db, $info )as $member){
                                        array_push($value["hijos"],  json_decode($member->getContenido(),TRUE));
                                    }
                                    
                                    
                                }
                            }
                            foreach ($value['attributes'] as $key2 => $value2) {
                                $this->texto.=" $key2='$value2'";
                            }
                        }
                    }
                    if ($key != "br") {
                        $this->texto.=">";
                    } else {
                        $this->texto.=" />";
                    }
                    if (array_key_exists('hijos', $value)) {
                        foreach ($value['hijos'] as $key1 => $value1) {
                            $this->fromMixed($value1, /* $texto, */ $x);
                        }
                    }
                } else {
                    if ($key != "br") {
                        $this->texto.=">";
                    } else {
                        $this->texto.=" />";
                    }
                }
                if ($key != "meta" && $key != "input" && $key != "br") {
                    $this->texto.="</$key>";
                }
            }
        }
    }

    protected
            function array2html($divn, $arr) {
        $div = array();
        $div['div']['attributes'] = array();
        $div['div']['attributes']['id'] = $divn;
        $div['div']['hijos'] = array();
        array_push($div['div']['hijos'], $arr);
        return $this->json2html(json_encode($div));
    }

}

?>
